<template>
  <a-spin :spinning="loading">
    <a-form v-bind="formItemLayout">
      <a-row>
        <a-col :span="12">
          <a-form-item label="订单号" v-bind="validateInfos.orderNo">
            <a-input v-model:value="formData.orderNo" placeholder="请输入订单号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保单号" v-bind="validateInfos.policyNo">
            <a-input v-model:value="formData.policyNo" placeholder="请输入保单号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="下单时间" v-bind="validateInfos.policyInsurDate">
		        <a-date-picker placeholder="请选择下单时间"  v-model:value="formData.policyInsurDate"  value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="区域" v-bind="validateInfos.areaId">
	          <j-select-dept v-model:value="formData.areaId" :multiple="true" checkStrictly :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险种类" v-bind="validateInfos.insuranceType">
	          <j-dict-select-tag v-model:value="formData.insuranceType" dictCode="BXZL" placeholder="请选择保险种类" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="项目名称" v-bind="validateInfos.projectName">
            <a-input v-model:value="formData.projectName" placeholder="请输入项目名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="项目编号" v-bind="validateInfos.projectId">
            <a-input v-model:value="formData.projectId" placeholder="请输入项目编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="被保人名称" v-bind="validateInfos.insuredName">
            <a-input v-model:value="formData.insuredName" placeholder="请输入被保人名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投保人名称" v-bind="validateInfos.policyHolderName">
            <a-input v-model:value="formData.policyHolderName" placeholder="请输入投保人名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投保联系人" v-bind="validateInfos.policyLinkmanName">
            <a-input v-model:value="formData.policyLinkmanName" placeholder="请输入投保联系人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投保联系人电话" v-bind="validateInfos.policyLinkmanMobilename">
            <a-input v-model:value="formData.policyLinkmanMobilename" placeholder="请输入投保联系人电话" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保费" v-bind="validateInfos.premium">
	          <a-input-number v-model:value="formData.premium" placeholder="请输入保费" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险状态" v-bind="validateInfos.policyState">
	          <j-dict-select-tag v-model:value="formData.policyState" dictCode="BXZT" placeholder="请选择保险状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="发票状态" v-bind="validateInfos.invoiceState">
            <j-dict-select-tag v-model:value="formData.invoiceState" dictCode="FPZT" placeholder="请选择发票状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险公司" v-bind="validateInfos.insuranceCompany">
	          <j-dict-select-tag v-model:value="formData.insuranceCompany" dictCode="BXGS" placeholder="请选择保险公司" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="付款状态" v-bind="validateInfos.payState">
	          <j-dict-select-tag v-model:value="formData.payState" dictCode="ZFZT" placeholder="请选择付款状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="邀请码" v-bind="validateInfos.invitationCode">
            <a-input v-model:value="formData.invitationCode" placeholder="请输入邀请码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="支付时间" v-bind="validateInfos.payTime">
            <a-date-picker placeholder="请选择支付时间"  v-model:value="formData.payTime"  value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="指令编号" v-bind="validateInfos.orderFlowNo">
            <a-input v-model:value="formData.orderFlowNo" placeholder="请输入指令编号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
		<!-- 子表单区域 -->
    <a-tabs v-model:activeKey="activeKey" animated>
      <a-tab-pane tab="保单" key="insurancePolicy" :forceRender="true">
        <insurance-policy-form ref="insurancePolicyFormRef" :disabled="disabled"></insurance-policy-form>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script lang="ts">
  import { defineComponent, ref, reactive, computed, toRaw, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useValidateAntFormAndTable } from '/@/hooks/system/useJvxeMethods';
  import { queryDataById, saveOrUpdate } from '../InsuranceOrder.api';
  import InsurancePolicyForm from './InsurancePolicyForm.vue'
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectDept from '/@/components/Form/src/jeecg/components/JSelectDept.vue';
  import { Form } from 'ant-design-vue';
  const useForm = Form.useForm;

  export default defineComponent({
    name: "InsuranceOrderForm",
    components:{
      JDictSelectTag,
      JSelectDept,
      InsurancePolicyForm,
    },
    props:{
      formDisabled:{
        type: Boolean,
        default: false
      },
      formData: { type: Object, default: ()=>{} },
      formBpm: { type: Boolean, default: true }
    },
    emits:['success'],
    setup(props, {emit}) {
      const loading = ref(false);
      const insurancePolicyFormRef = ref();
      const activeKey = ref('insurancePolicy');
      const formData = reactive<Record<string, any>>({
        id: '',
        orderNo: '',
        policyNo: '',
        policyInsurDate: '',
        areaId: '',
        insuranceType: '',
        projectName: '',
        projectId: '',
        insuredName: '',
        policyHolderName: '',
        policyLinkmanName: '',
        policyLinkmanMobilename: '',
        premium: '',
        policyState: '',
        insuranceCompany: '',
        payState: '',
        payTime: '',
        orderFlowNo: '',
        invitationCode: '',
      });

      //表单验证
      const validatorRules = reactive({
        orderNo: [{ required: true, message: '请输入订单号!'},],
        areaId: [{ required: true, message: '请输入区域!'},],
        insuranceType: [{ required: true, message: '请输入保险种类!'},],
        projectName: [{ required: true, message: '请输入项目名称!'},],
        projectId: [{ required: true, message: '请输入项目编号!'},],
        insuredName: [{ required: true, message: '请输入被保人名称!'},],
        policyHolderName: [{ required: true, message: '请输入投保人名称!'},],
        premium: [{ required: true, message: '请输入保费!'},],
        policyState: [{ required: true, message: '请输入保险状态!'},],
        insuranceCompany: [{ required: true, message: '请输入保险公司!'},],
        payState: [{ required: true, message: '请输入付款状态!'},],
        payTime: [{ required: false, message: '请输入付款状态!'},],
        orderFlowNo: [{ required: false, message: '请输入付款状态!'},]
      });
      const {resetFields, validate, validateInfos} = useForm(formData, validatorRules, {immediate: true});
      const dbData = {};
      const formItemLayout = {
        labelCol: {xs: {span: 24}, sm: {span: 5}},
        wrapperCol: {xs: {span: 24}, sm: {span: 16}},
      };

      // 表单禁用
      const disabled = computed(()=>{
        if(props.formBpm === true){
          if(props.formData.disabled === false){
            return false;
          }else{
            return true;
          }
        }
        return props.formDisabled;
      });

      

      function add() {
        resetFields();
        insurancePolicyFormRef.value.initFormData();
      }

      async function edit(row) {
        //主表数据
        await queryMainData(row.id);
        //子表数据
        await insurancePolicyFormRef.value.initFormData(row['id']);
      }

      async function queryMainData(id) {
        const row = await queryDataById(id);
        Object.keys(row).map(k => {
          formData[k] = row[k];
        });
      }

      const {getSubFormAndTableData, transformData} = useValidateAntFormAndTable(activeKey, {
        'insurancePolicy': insurancePolicyFormRef,
      });

      async function getFormData() {
        await validate();
        return transformData(toRaw(formData))
      }

      async function submitForm() {
        const mainData = await getFormData();
        const subData = await getSubFormAndTableData();
        const values = Object.assign({}, dbData, mainData, subData);
        console.log('表单提交数据', values)
        const isUpdate = values.id ? true : false
        await saveOrUpdate(values, isUpdate);
        //关闭弹窗
        emit('success');
      }
      
      function setFieldsValue(values) {
        if(values){
          Object.keys(values).map(k=>{
            formData[k] = values[k];
          });
        }
      }

      /**
       * 值改变事件触发-树控件回调
       * @param key
       * @param value
       */
      function handleFormChange(key, value) {
        formData[key] = value;
      }


      return {
        insurancePolicyFormRef,
        validatorRules,
        validateInfos,
        activeKey,
        loading,
        formData,
        setFieldsValue,
        handleFormChange,
        formItemLayout,
        disabled,
        getFormData,
        submitForm,
        add,
        edit
      }
    }
  });
</script>
